package _10_8;

import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;

public class Main {

    public static void displayMenu() {
	System.out.println("Menu:");
	System.out.println("\t1)iso-8859-5 to UTF-8");
	System.out.println("\t2)UTF-8 to iso-8859-5");
	System.out.print("Enter your choice: ");
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
	try {
	    Scanner scanner = new Scanner(System.in);
	    int choice;

	    displayMenu();
	    if ((choice = scanner.nextInt()) != 1 && choice != 2) {
		throw new Exception("Invalid menu choice.");
	    }

	    System.out.print("Enter the name of the file you wish to convert: ");
	    String fileName = scanner.next();

	    scanner = new Scanner(new File(fileName));
	    FileWriter fileWriter = new FileWriter("converted-" + fileName);

	    while (scanner.hasNextLine()) {
		String line = scanner.nextLine();
		byte[] bytes = line.getBytes();
		fileWriter.write(new String(bytes, (choice == 1 ? "UTF-8" : "iso-8859-5")) + "\n");
	    }

	    // fileWriter.flush();
	    fileWriter.close();
	    System.out.println("Done.");
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
}
